package com.hycreate.platform.modules.delay;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hycreate.platform.common.config.Global;
import com.hycreate.platform.common.persistence.Page;
import com.hycreate.platform.common.utils.DateDayUtil;
import com.hycreate.platform.common.utils.StringUtils;
import com.hycreate.platform.common.web.BaseController;
import com.hycreate.platform.modules.numberinfo.YhcgNumberinfo;
import com.hycreate.platform.modules.numberinfo.YhcgNumberinfoService;
import com.hycreate.platform.modules.sys.entity.Office;
import com.hycreate.platform.modules.sys.entity.User;
import com.hycreate.platform.modules.sys.service.OfficeService;
import com.hycreate.platform.modules.sys.utils.UserUtils;

/**
 * 延期记录Controller
 * @author wang
 * @version 2017-11-30
 */
@Controller
@RequestMapping(value = "${adminPath}/delay/yhcgDelay")
public class YhcgDelayController extends BaseController {

	@Autowired
	private YhcgDelayService yhcgDelayService;
	
	@Autowired
	private YhcgNumberinfoService yhcgNumberinfoService;
	@Autowired 
	private OfficeService officeService;//注入责任单位及督办单位

	@ModelAttribute
	public YhcgDelay get(@RequestParam(required=false) String id) {
		YhcgDelay entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = yhcgDelayService.get(id);
		}
		if (entity == null){
			entity = new YhcgDelay();
		}
		return entity;
	}
	
	@RequiresPermissions("delay:yhcgDelay:view")
	@RequestMapping(value = {"list", ""})
	public String list(YhcgDelay yhcgDelay, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<YhcgDelay> page = yhcgDelayService.findPage(new Page<YhcgDelay>(request, response), yhcgDelay); 
		User us=UserUtils.getUser();
		Office office=new Office();
		office.setType("4");
		List<Office> list=officeService.findListall(office);//查询处理单位,处理单位为中队级
		office.setType("3");
		List<Office> list1=officeService.findListall(office);//查询督办单位，督办单位未大队级
		model.addAttribute("list", list);
		model.addAttribute("list1", list1);
		model.addAttribute("page", page);
		model.addAttribute("us", us);//封装当前用户
		return "modules/delay/yhcgDelayList";
	}
	
	@RequiresPermissions("delay:yhcgDelay:view")
	@RequestMapping(value = "form")
	public String form(YhcgDelay yhcgDelay, Model model) {
		model.addAttribute("yhcgDelay", yhcgDelay);
		return "modules/delay/yhcgDelayForm";
	}

	@RequestMapping(value = "formcha",method=RequestMethod.POST)
	@ResponseBody
	public YhcgDelay formcha(YhcgDelay yhcgDelay, Model model) {
		yhcgDelay= yhcgDelayService.get(yhcgDelay.getId());
		return yhcgDelay;
	}
	
	@RequiresPermissions("delay:yhcgDelay:edit")
	@RequestMapping(value = "save")
	public String save(YhcgDelay yhcgDelay, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, yhcgDelay)){
			return form(yhcgDelay, model);
		}
		yhcgDelayService.save(yhcgDelay);
		addMessage(redirectAttributes, "保存延期成功");
		return "redirect:"+Global.getAdminPath()+"/delay/yhcgDelay/?repage";
	}
	/**
	 * 延期审核通过
	 * @param yhcgDelay
	 * @param model
	 * @param redirectAttributes
	 * @return
	 * @throws java.text.ParseException 
	 * @throws ParseException 
	 * @throws NumberFormatException 
	 */
	@RequiresPermissions("delay:yhcgDelay:edit")
	@RequestMapping(value = "fromdataguo")
	public String fromdataguo(YhcgDelay yhcgDelay, Model model, RedirectAttributes redirectAttributes) throws NumberFormatException, java.text.ParseException {
		if (!beanValidator(model, yhcgDelay)){
			return form(yhcgDelay, model);
		}
		yhcgDelay.setShentime(new Date());//封装审核时间
		yhcgDelayService.save(yhcgDelay);
		YhcgNumberinfo yhcgNumberinfo=yhcgNumberinfoService.get(yhcgDelay.getUid());//根据工单id查询工单信息、
		DateDayUtil ddu=new DateDayUtil();
		SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	    String httpArg=f.format(yhcgNumberinfo.getTimelimit());
	    String date= ddu.DateUtil(httpArg.substring(0, 10), Integer.parseInt(yhcgDelay.getExtensiondays()));//查询工作日后的天数
	    yhcgNumberinfo.setTimelimit(f.parse(date+httpArg.substring(10, httpArg.length())));//保存审核通过后的截止日
	    yhcgNumberinfoService.save(yhcgNumberinfo);
		addMessage(redirectAttributes, "审核成功");
		return "redirect:"+Global.getAdminPath()+"/delay/yhcgDelay/?repage";
	}
	
	@RequiresPermissions("delay:yhcgDelay:edit")
	@RequestMapping(value = "fromdataju")
	public String fromdataju(YhcgDelay yhcgDelay, Model model, RedirectAttributes redirectAttributes,@RequestParam String yuan) {
		if (!beanValidator(model, yhcgDelay)){
			return form(yhcgDelay, model);
		}
		yhcgDelay.setStatus("2");//保存状态为拒绝
		yhcgDelay.setShentime(new Date());//封装审核时间
		yhcgDelay.setJu(yuan);
		User us=UserUtils.getUser();
		yhcgDelay.setShenuser(us.getId());//封装审核人
		yhcgDelayService.save(yhcgDelay);
		addMessage(redirectAttributes, "延期审核拒绝成功");
		return "redirect:"+Global.getAdminPath()+"/delay/yhcgDelay/?repage";
	}
	
	@RequiresPermissions("delay:yhcgDelay:edit")
	@RequestMapping(value = "delete")
	public String delete(YhcgDelay yhcgDelay, RedirectAttributes redirectAttributes) {
		yhcgDelayService.delete(yhcgDelay);
		addMessage(redirectAttributes, "删除延期成功");
		return "redirect:"+Global.getAdminPath()+"/delay/yhcgDelay/?repage";
	}
	/**
	 * 查看页面加载信息
	 */
	@RequestMapping(value = "listcha",method=RequestMethod.POST)
	@ResponseBody
	public List<YhcgDelay> listcha(YhcgDelay yhcgDelay, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<YhcgDelay> page = yhcgDelayService.findAllList(yhcgDelay); 
		return page;
	}
}